package net.hn.hnms.biz.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.ContractInvoice;
import net.hn.hnms.biz.project.domain.ContractPayment;
import net.hn.hnms.biz.project.request.ContractInfoReq;
import net.hn.hnms.biz.project.request.ContractInvoiceReq;
import net.hn.hnms.biz.project.vo.ContractInfoVo;
import net.hn.hnms.biz.project.vo.ContractInvoiceVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ContractInvoiceMapper extends BaseMapper<ContractInvoice> {
    @Update("<script>" +
            " update contract_invoice set  del_flag = '1' " +
            " where contract_invoice_id in" +
            " <foreach collection=\"list\" item=\"value\" separator=\",\" open=\"(\" close=\")\">  " +
            "#{value}" +
            "</foreach>" +
            "</script>")
    void updateDeleteByIds(@Param("list") List<String> list);

    @Select("<script>" +
            "select a.*, pi.project_name projectName from (SELECT  " +
            " ci.contract_id contractId, " +
            " ci.contract_code contractCode,  " +
            " ci.contract_title contractTitle,  " +
            " ci.project_id projectId,  " +
            " ci.total_amount totalAmount,  " +
            " ci.contract_date contractDate,  " +
            " ci.legal_person_id legalPersonId,  " +
            " ci.legal_person_code legalPersonCode,  " +
            " ci.legal_person_name legalPersonName,  " +
            " ci.supplier_id supplierId,  " +
            " ci.supplier_code supplierCode,  " +
            " ci.supplier_name supplierName,  " +
            " ci.contract_type contractType,  " +
            " ci.contract_seal contractSeal,  " +
            " ci.contract_state contractState,  " +
            " ci.contract_file_name contractFileName,  " +
            " ci.contract_file_path contractFilePath,  " +
            " ci.create_time createTime,  " +
            " count(cin.contract_invoice_id) invoiceCount  " +
            " FROM  " +
            " contract_info ci  " +
            " LEFT JOIN contract_invoice cin ON ci.contract_id = cin.contract_id  " +
            " AND cin.del_flag = '0'   " +
            " WHERE  " +
            " ci.del_flag = '0'   " +
            " <if test='query.mineCode != null and query.mineCode != \"\" '> "+
            " and ci.mine_code = #{query.mineCode} " +
            " </if>"+
            " <if test='query.contractCode!= null and query.contractCode!= \"\" '>"+
            " and ci.contract_code LIKE CONCAT('%', #{query.contractCode}, '%') " +
            " </if>"+
            " <if test='query.contractTitle!= null and query.contractTitle!= \"\" '>"+
            " and ci.contract_title LIKE CONCAT('%', #{query.contractTitle}, '%') " +
            " </if>"+
            " <if test='query.totalAmount!= null and query.totalAmount!= \"\" '>"+
            " and ci.total_amount >= #{query.totalAmount} " +
            " </if>"+
            " GROUP BY  " +
            " ci.contract_id ) a " +
            " left join project_info pi on a.projectId = pi.project_id and pi.del_flag = '0' </script>")
    Page<ContractInfoVo> selectContractPageList(Page<Object> build, @Param("query") ContractInfoReq query);

    @Select("<script>" +
            "select " +
            "contract_payment_id contractPaymentId," +
            "payment_code paymentCode," +
            "payment_amount paymentAmount," +
            "payment_title paymentTitle," +
            "payment_state paymentState " +
            "from contract_payment " +
            "where del_flag = '0' " +
            "<if test='query.mineCode != null and query.mineCode != \"\" '> "+
            "and mine_code = #{query.mineCode} " +
            "</if>"+
            "</script>")
    List<ContractPayment> getPayList(@Param("query") ContractInvoiceReq query);


    @Select("<script>" +
            " SELECT  " +
            " ci.contract_invoice_id contractInvoiceId, " +
            " ci.invoice_code invoiceCode, " +
            " ci.invoice_company invoiceCompany, " +
            " ci.invoice_type invoiceType, " +
            " ci.invoice_amount invoiceAmount, " +
            " ci.invoice_date invoiceDate, " +
            " ci.invoice_file_path invoiceFilePath, " +
            " ci.invoice_file_name invoiceFileName, " +
            " ci.create_time createTime, " +
            " ci.contract_id contractId, " +
            " ci.type, " +
            " ci.oss_id ossId, " +
            " cp.payment_code paymentCode, " +
            " cp.payment_amount paymentAmount, " +
            " cp.payment_title paymentTitle, " +
            " cp.payment_state paymentState, " +
            " cp.contract_payment_id contractPaymentId " +
            " FROM  " +
            " contract_invoice ci  " +
            " LEFT JOIN contract_invoice_payment cip ON ci.contract_invoice_id = cip.invoice_id  " +
            " left join contract_payment cp on cip.payment_id = cp.contract_payment_id" +
            " AND cp.del_flag = '0'   " +
            " WHERE  " +
            " ci.del_flag = '0' and ci.contract_id = #{query.contractId} " +
            "  </script>")
    Page<ContractInvoiceVo> selectPage(Page<Object> build, @Param("query") ContractInvoiceReq query);
}
